package cn.eric.framework.ajax;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

public class EAjaxAdvice implements MethodBeforeAdvice{
    private Logger logger;

    public EAjaxAdvice(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        List<String> urls = Arrays.asList(method.getAnnotation(RequestMapping.class).value());
        logger.log(Level.INFO, String.format("[REQUEST] url : %s", urls.toString()));
        StringBuilder sb = new StringBuilder();
        for(Object _ : objects) {
            sb.append(_.getClass().cast(_).toString());
        }
        logger.log(Level.INFO, String.format("[REQUEST] args %s", sb.toString()));
    }
}
